#encoding:utf-8
'''
FileName:sharpfssync.py
Creator:王烨
Date:2010.5.29
'''
import hashlib
from rpc import ChannelSync, Proxy
from common_pb2 import *
import auth_pb2
import filesystem_pb2
import io_pb2
from error import SharpFsError

class SharpFsSync(object):
    ErrorMsg = {TOKEN_EXPIRED : u'token过期',
                USER_EXISTS : u'用户已存在',
                USER_NOT_EXISTS : u'用户不存在',
                INCORRECT_PASSWORD : u'密码错误', 
                INVALID_PERMISSION : u'权限错误',
                FILE_EXISTS : u'文件已存在',
                FILE_NOT_EXISTS : u'文件不存在',
                DIR_EXISTS : u'文件夹已存在',
                DIR_NOT_EXISTS : u'文件夹不存在',
                DIR_NOT_EMPTY : u'文件夹不为空'}
    
    def __init__(self):
        self.channel = ChannelSync()
        self.proxy = Proxy(auth_pb2.Authentication_Stub(self.channel), \
                           filesystem_pb2.FileSystem_Stub(self.channel),\
                           io_pb2.Io_Stub(self.channel))
    
    def Connect(self, host, port):
        self.channel.Connect((host, port))
        
    def Close(self):
        self.channel.Close()
        
    def __CheckError(self, result, msg = None):
        if result.HasField('error'):
            if msg == None:
                msg = self.ErrorMsg[result.error]
            raise SharpFsError(result.error, msg)
                
    def Login(self, name, password):
        p = auth_pb2.LoginParameters()
        p.name = name
        m = hashlib.md5()
        m.update(password)
        p.password = m.hexdigest()
        result = self.proxy.Authentication.Login(p)
        self.__CheckError(result)
        return result
    
    def Logout(self, token):
        p = auth_pb2.LogoutParameters()
        p.token = token
        result = self.proxy.Authentication.Logout(p)
        self.__CheckError(result)
        
    def Kick(self, token, tokenToKick):
        p = auth_pb2.KickParameters()
        p.token = token
        p.tokenToKick = tokenToKick
        self.proxy.Authentication.Kick(p)
    
    def CreateUser(self, name, password, realName, email):
        p = auth_pb2.CreateUserParameters()
        p.name = name
        m = hashlib.md5()
        m.update(password)
        p.password = unicode(m.hexdigest())
        p.realName = realName
        p.email = email
        result = self.proxy.Authentication.CreateUser(p)
        self.__CheckError(result)
        
    def DeleteUser(self, token, userName):
        p = auth_pb2.DeleteUserParameters()
        p.token = token
        p.userName = userName
        result = self.proxy.Authentication.DeleteUser(p)
        self.__CheckError(result)
        
    def ChangePassword(self, token, name, password):
        p = auth_pb2.ChangePasswordParameters()
        p.token = token
        m = hashlib.md5()
        m.update(password)
        p.newPassword = unicode(m.hexdigest())
        if name != None:
            p.name = name
        result = self.proxy.Authentication.ChangePassword(p)
        self.__CheckError(result) 
        
    def ListOnlineUsers(self, token):
        p = auth_pb2.ListAllUsersParameters()
        p.token = token
        result = self.proxy.Authentication.ListOnlineUsers(p)
        self.__CheckError(result)
        return result
        
    def ListAllUsers(self, token):
        p = auth_pb2.ListAllUsersParameters()
        p.token = token
        result = self.proxy.Authentication.ListAllUsers(p)
        self.__CheckError(result)
        return result
    
    def List(self, token, path, onlyDir):
        p = filesystem_pb2.ListParameters()
        p.token = token
        p.path = path
        p.onlyDir = onlyDir
        result = self.proxy.FileSystem.List(p)
        self.__CheckError(result)
        return result
    
    def Move(self, token, oldPath, newPath):
        p = filesystem_pb2.MoveParameters()
        p.token = token
        p.oldPath = oldPath
        p.newPath = newPath
        result = self.proxy.FileSystem.Move(p)
        self.__CheckError(result)
        return result
    
    def CreateDir(self, token, path):
        p = filesystem_pb2.CreateDirParameters()
        p.token = token
        p.path = path
        result = self.proxy.FileSystem.CreateDir(p)
        self.__CheckError(result)
        return result
    
    def Delete(self, token, path):
        p = filesystem_pb2.CreateDirParameters()
        p.token = token
        p.path = path
        result = self.proxy.FileSystem.Delete(p)
        self.__CheckError(result)
        return result
    
    def CreateFile(self, token, path):
        p = io_pb2.CreateFileParameters()
        p.token = token
        p.path = path
        result = self.proxy.Io.CreateFile(p)
        self.__CheckError(result)
        return result
    
    def AddBlock(self, token, inode):
        p = io_pb2.AddBlockParameters()
        p.token = token
        p.inode = inode
        result = self.proxy.Io.AddBlock(p)
        self.__CheckError(result)
        return result
    
    def OpenFile(self, token, path, isRead = True):
        p = io_pb2.OpenFileParameters()
        p.token = token
        p.path = path
        if isRead:
            p.mode = io_pb2.READ
        else:
            p.mode = io_pb2.APPEND
        result = self.proxy.Io.OpenFile(p)
        self.__CheckError(result)
        return result